home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / XINTRO18.ZIP / LIB.C next >
C/C++ Source or Header  |  1993-11-25  |  9KB  |  350 lines

  1. /*
  2.  * LIB.C v1.2
  3.  *
  4.  * by Robert Schmidt
  5.  * (C)1993 Ztiff Zox Softwear
  6.  *
  7.  * Simple graphics library to accompany the article
  8.  * 
  9.  *            INTRODUCTION TO MODE X.
  10.  * 
  11.  * This library provides the basic functions for initializing and using
  12.  * unchained (planar) 256-color VGA modes.  Currently supported are:
  13.  *
  14.  *    - 320x200
  15.  *    - 320x240
  16.  *
  17.  * Functions are provided for:
  18.  *
  19.  *    - initializing one of the available modes
  20.  *    - setting the start address of the VGA refresh data
  21.  *    - setting active and visible display pages
  22.  *    - writing and reading a single pixel to/from video memory
  23.  *
  24.  * The library is provided as a demonstration only, and is not claimed
  25.  * to be particularly efficient or suited for any purpose.  It has only
  26.  * been tested with Borland C++ 3.1 by the author.  Comments on success
  27.  * or disaster with other compilers are welcome.
  28.  *
  29.  * This file is public domain.  Do with it whatever you'd like, but
  30.  * please don't distribute it without the article.
  31.  *
  32.  * Thanks go out to various helpful netters who spotted the 0xE7 bug
  33.  * in the set320x240x256() function!
  34.  */
  35.  
  36.  
  37. /*
  38.  * We 'require' a large data model simply to get rid of explicit 'far'
  39.  * pointers and compiler specific '_fmemset()' functions and the likes.
  40.  */
  41.  
  42. #if !defined(__COMPACT__)
  43. # if !defined(__LARGE__)
  44. #  if !defined(__HUGE__)
  45. #   error Large data model required!  Try compiling with 'bcc -ml lib.c'.
  46. #  endif
  47. # endif
  48. #endif
  49.  
  50. #include <dos.h>
  51. #include <mem.h>
  52.  
  53. /*
  54.  * Comment out the following #define if you don't want the testing main()
  55.  * to be included.
  56.  */
  57.  
  58. #define TESTING
  59.  
  60. /*
  61.  * Define the port addresses of some VGA registers.
  62.  */
  63.  
  64. #define CRTC_ADDR    0x3d4    /* Base port of the CRT Controller (color) */
  65.  
  66. #define SEQU_ADDR    0x3c4    /* Base port of the Sequencer */
  67. #define GRAC_ADDR    0x3ce    /* Base port of the Graphics Controller */
  68.  
  69.  
  70. /*
  71.  * Make a far pointer to the VGA graphics buffer segment.  Your compiler
  72.  * might not have the MK_FP macro, but you'll figure something out.
  73.  */
  74.  
  75. typedef unsigned char UCHAR;
  76. UCHAR *vga = (UCHAR *) MK_FP(0xA000, 0);
  77.  
  78. /*
  79.  * width and height should specify the mode dimensions.  widthBytes
  80.  * specify the width of a line in addressable bytes.
  81.  */
  82.  
  83. unsigned width, height, widthBytes;
  84.  
  85. /*
  86.  * actStart specifies the start of the page being accessed by
  87.  * drawing operations.  visStart specifies the contents of the Screen
  88.  * Start register, i.e. the start of the visible page.
  89.  */
  90.  
  91. unsigned actStart, visStart;
  92.  
  93. /*
  94.  * set320x200x256_X()
  95.  *    sets mode 13h, then turns it into an unchained (planar), 4-page
  96.  *    320x200x256 mode.
  97.  */
  98.  
  99. void set320x200x256_X(void)
  100.     {
  101.  
  102.     union REGS r;
  103.  
  104.     /* Set VGA BIOS mode 13h: */
  105.  
  106.     r.x.ax = 0x0013;
  107.     int86(0x10, &r, &r);
  108.  
  109.     /* Turn off the Chain-4 bit (bit 3 at index 4, port 0x3c4): */
  110.  
  111.     outport(SEQU_ADDR, 0x0604);
  112.  
  113.     /* Turn off word mode, by setting the Mode Control register
  114.        of the CRT Controller (index 0x17, port 0x3d4): */
  115.  
  116.     outport(CRTC_ADDR, 0xE317);
  117.  
  118.     /* Turn off doubleword mode, by setting the Underline Location
  119.        register (index 0x14, port 0x3d4): */
  120.  
  121.     outport(CRTC_ADDR, 0x0014);
  122.  
  123.     /* Clear entire video memory, by selecting all four planes, then
  124.        writing 0 to entire segment. */
  125.  
  126.     outport(SEQU_ADDR, 0x0F02);
  127.     memset(vga+1, 0, 0xffff); /* stupid size_t exactly 1 too small */
  128.     vga[0] = 0;
  129.  
  130.     /* Update the global variables to reflect dimensions of this
  131.        mode.  This is needed by most future drawing operations. */
  132.  
  133.     width   = 320;
  134.     height    = 200;
  135.  
  136.     /* Each byte addresses four pixels, so the width of a scan line
  137.        in *bytes* is one fourth of the number of pixels on a line. */
  138.  
  139.     widthBytes = width / 4;
  140.  
  141.     /* By default we want screen refreshing and drawing operations
  142.        to be based at offset 0 in the video segment. */
  143.  
  144.     actStart = visStart = 0;
  145.  
  146.     }
  147.  
  148. /*
  149.  * setActiveStart() tells our graphics operations which address in video
  150.  * memory should be considered the top left corner.
  151.  */
  152.  
  153. void setActiveStart(unsigned offset)
  154.     {
  155.     actStart = offset;
  156.     }
  157.  
  158. /*
  159.  * setVisibleStart() tells the VGA from which byte to fetch the first
  160.  * pixel when starting refresh at the top of the screen.  This version
  161.  * won't look very well in time critical situations (games for
  162.  * instance) as the register outputs are not synchronized with the
  163.  * screen refresh.  This refresh might start when the high byte is
  164.  * set, but before the low byte is set, which produces a bad flicker.
  165.  */
  166.  
  167. void setVisibleStart(unsigned offset)
  168.     {
  169.     visStart = offset;
  170.     outport(CRTC_ADDR, 0x0C);        /* set high byte */
  171.     outport(CRTC_ADDR+1, visStart >> 8);
  172.     outport(CRTC_ADDR, 0x0D);        /* set low byte */
  173.     outport(CRTC_ADDR+1, visStart & 0xff);
  174.     }
  175.  
  176. /*
  177.  * setXXXPage() sets the specified page by multiplying the page number
  178.  * with the size of one page at the current resolution, then handing the
  179.  * resulting offset value over to the corresponding setXXXStart()
  180.  * function.  The first page is number 0.
  181.  */
  182.  
  183. void setActivePage(int page)
  184.     {
  185.     setActiveStart(page * widthBytes * height);
  186.     }
  187.  
  188. void setVisiblePage(int page)
  189.     {
  190.     setVisibleStart(page * widthBytes * height);
  191.     }
  192.  
  193. void putPixel_X(int x, int y, UCHAR color)
  194.     {
  195.  
  196.     /* Each address accesses four neighboring pixels, so set
  197.        Write Plane Enable according to which pixel we want
  198.        to modify.  The plane is determined by the two least
  199.        significant bits of the x-coordinate: */
  200.  
  201.     outportb(0x3c4, 0x02);
  202.     outportb(0x3c5, 0x01 << (x & 3));
  203.  
  204.     /* The offset of the pixel into the video segment is
  205.        offset = (width * y + x) / 4, and write the given
  206.        color to the plane we selected above.  Heed the active
  207.        page start selection. */
  208.  
  209.     vga[(unsigned)(widthBytes * y) + (x / 4) + actStart] = color;
  210.  
  211.     }
  212.  
  213. UCHAR getPixel_X(int x, int y)
  214.     {
  215.  
  216.     /* Select the plane from which we must read the pixel color: */
  217.  
  218.     outport(GRAC_ADDR, 0x04);
  219.     outport(GRAC_ADDR+1, x & 3);
  220.  
  221.     return vga[(unsigned)(widthBytes * y) + (x / 4) + actStart];
  222.  
  223.     }
  224.  
  225. void set320x240x256_X(void)
  226.     {
  227.  
  228.     /* Set the unchained version of mode 13h: */
  229.  
  230.     set320x200x256_X();
  231.  
  232.     /* Modify the vertical sync polarity bits in the Misc. Output
  233.        Register to achieve square aspect ratio: */
  234.  
  235.     outportb(0x3C2, 0xE3);
  236.  
  237.     /* Modify the vertical timing registers to reflect the increased
  238.        vertical resolution, and to center the image as good as
  239.        possible: */
  240.  
  241.     outport(0x3D4, 0x2C11);        /* turn off write protect */
  242.     outport(0x3D4, 0x0D06);        /* vertical total */
  243.     outport(0x3D4, 0x3E07);        /* overflow register */
  244.     outport(0x3D4, 0xEA10);        /* vertical retrace start */
  245.     outport(0x3D4, 0xAC11);        /* vertical retrace end AND wr.prot */
  246.     outport(0x3D4, 0xDF12);        /* vertical display enable end */
  247.     outport(0x3D4, 0xE715);        /* start vertical blanking */
  248.     outport(0x3D4, 0x0616);        /* end vertical blanking */
  249.  
  250.     /* Update mode info, so future operations are aware of the
  251.        resolution */
  252.  
  253.     height = 240;
  254.  
  255.     }
  256.  
  257.  
  258.  
  259. /*
  260.  * The library testing routines follows below.
  261.  */
  262.  
  263.  
  264. #ifdef TESTING
  265.  
  266. #include <stdio.h>
  267. #include <conio.h>
  268.  
  269. void set80x25(void)
  270.     {
  271.     union REGS r;
  272.     r.x.ax = 0x0003;
  273.     int86(0x10, &r, &r);
  274.     }
  275.  
  276. void doTest(void)
  277.     {
  278.     int p, x, y, pages;
  279.  
  280.     /* This is the way to calculate the number of pages available. */
  281.  
  282.     pages = 65536L/(widthBytes*height);
  283.  
  284.     for (p = 0; p < pages; ++p)
  285.         {
  286.         setActivePage(p);
  287.  
  288.         /* On each page draw a single colored border, and dump the palette
  289.            onto a small square about the middle of the page. */
  290.  
  291.         for (x = 0; x <= width; ++x)
  292.             {
  293.             putPixel_X(x, 0, p+1);
  294.             putPixel_X(x, height-1, p+1);
  295.             }
  296.  
  297.         for (y = 0; y <= height; ++y)
  298.             {
  299.             putPixel_X(0, y, p+1);
  300.             putPixel_X(width-1, y, p+1);
  301.             }
  302.  
  303.         for (x = 0; x < 16; ++x)
  304.             for (y = 0; y < 16; ++y)
  305.                 putPixel_X(x+(p+3)*16, y+(p+3)*16, x + y*16);
  306.  
  307.         }
  308.  
  309.     /* Each pages will now contain a different image.  Let the user cycle
  310.        through all the pages by pressing a key. */
  311.  
  312.     for (p = 0; p < pages; ++p)
  313.         {
  314.         setVisiblePage(p);
  315.         getch();
  316.         }
  317.  
  318.     }
  319.  
  320.  
  321. /*
  322.  * Library test (program) entry point.
  323.  */
  324.  
  325. int main(void)
  326.     {
  327.     puts("First, have a look at the 320x200 mode.  I will draw some rubbish");
  328.     puts("on all of the four pages, then let you cycle through them by");
  329.     puts("hitting a key on each page.");
  330.     puts("Press a key when ready...");
  331.     getch();
  332.  
  333.     set320x200x256_X();
  334.     doTest();
  335.  
  336.     set80x25();
  337.     puts("Then, check out Mode X, 320x240 with 3 (and a half) pages.");
  338.     puts("Press a key when ready...");
  339.     getch();
  340.  
  341.     set320x240x256_X();
  342.     doTest();
  343.  
  344.     set80x25();
  345.     puts("Where to next?  It's your move!");
  346.     return 0;
  347.     }
  348.  
  349. #endif
  350.